import time

from fastapi import Depends, FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse

from misc.auth import login_required, login_required_admin
from config import get_version


def init_app_routes(app: FastAPI):
    from routes.user import router as user_router
    from routes.news import router as news_router
    from routes.event import router as event_router
    from routes.edit import router as edit_router
    from routes.delete import router as delete_router
    from routes.admin import router as admin_router
    from routes.register import router as register_router
    from routes.recruit import router as recruit_router
    from routes.dingtalk import router as dingtalk_router
    from routes.interview import router as interview_router
    from routes.member import router as member_router
    from routes.upload import router as upload_router
    from routes.images import router as images_router

    app.include_router(
        news_router,
        prefix="/api/news",
        tags=["news"],
    )
    app.include_router(
        event_router,
        prefix="/api/event",
        tags=["event"],
    )
    app.include_router(
        user_router,
        prefix="/api/user",
        tags=["user"],
    )
    
    app.include_router(
        register_router,
        prefix="/api/register",
        tags=["register"],
    )
    
    app.include_router(
        recruit_router,
        prefix="/api/recruit",
        tags=["recruit"],
    )


    app.include_router(
        edit_router,
        prefix="/api/edit",
        tags=["edit"],
        dependencies=[Depends(login_required_admin)],
    )
    app.include_router(
        delete_router,
        prefix="/api/delete",
        tags=["delete"],
        dependencies=[Depends(login_required_admin)],
    )
    app.include_router(
        admin_router,
        prefix="/api/admin",
        tags=["admin"],
        dependencies=[Depends(login_required_admin)],
    )
    
    app.include_router(
        dingtalk_router,
        prefix="/api/dingtalk",
        tags=["dingtalk"],
        dependencies=[Depends(login_required_admin)],
    )
    
    app.include_router(
        interview_router,
        prefix="/api/interview",
        tags=["interview"],
        dependencies=[Depends(login_required_admin)],
    )
    
    app.include_router(
        member_router,
        prefix="/api/member",
        tags=["member"],
        dependencies=[Depends(login_required_admin)],
    )
    
    app.include_router(
        upload_router,
        prefix="/api/upload",
        tags=["upload"],
        dependencies=[Depends(login_required_admin)],
    )
    
    app.include_router(
        images_router,
        prefix="/api",
        tags=["images"],
    )

    @app.middleware("http")
    async def add_header(request: Request, call_next):
        start_time = time.time()
        response = await call_next(request)
        process_time = time.time() - start_time
        response.headers["X-Process-Time"] = str(round(process_time * 1000, 4))
        response.headers["X-CsaWeb-Version"] = get_version()
        return response

    @app.middleware("http")
    async def add_cache_control(request: Request, call_next):
        response = await call_next(request)
        if "Cache-Control" not in response.headers:
            for tp in ["image", "font", "css", "javascript"]:
                if tp in response.headers.get("Content-Type", ""):
                    response.headers["Cache-Control"] = "public, max-age=2592000"
                    break
            else:
                response.headers["Cache-Control"] = "no-store"
        return response

    app.add_middleware(
        CORSMiddleware,
        allow_origins="*",
        allow_credentials=True,
        allow_methods=["*"],
        allow_headers=["*"],
    )
